#!/usr/bin/python2.5
from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.ext.webapp import template

import os

class BaseHandler(webapp.RequestHandler):
    """Base RequestHandler"""         
    
    def render_to_response(self, filename, template_args):
        """Renders a Django template and sends it to the client.
        Args:
        filename: template path (relative to this file)
        template_args: argument dict for the template
        """    
        
        mobile_version = False            
        if "http://m.kindly.cz" in self.request.uri:
            mobile_version = True            
        
        template_args.setdefault('current_uri', self.request.uri)
        template_args.setdefault('mobile_version', mobile_version)
        template_path = os.path.join(os.path.dirname(__file__), "templates", filename)
        self.response.headers['Content-Type'] = 'text/html'
        self.response.out.write(template.render(template_path, template_args))
       
# Get the template Library
register = template.create_template_register()
template.register_template_library('basehandler')
        
@register.inclusion_tag('user_link.html')
def render_user_link(current_uri):
  """Renders a link that the user can use to log into or out of Google account.

  This is a custom tag since it's included in the header of virtually every
  page served by Image Sharing, and we don't want to force views to pass the
  user in the template context every time.

  Args:
    current_uri: the uri of the page currently being rendered

  Returns:
    A link that allows the user to log in or out.
  """
  user = users.get_current_user()

  # Note: Here we return a dict that serves at the context for the
  # template indicated in the method decorator.
  if user is None:
    url = users.create_login_url(current_uri)
    return {'authenticated': False,
            'username': "host",
            'url': url}
  else:
    url = users.create_logout_url(current_uri)
    return {'authenticated': True,
            'username': user.email(),
            'url': url} 